package com.heima.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.UserConstants;
import com.heima.feign.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.AuthMapper;
import com.heima.user.service.ApUserService;
import com.heima.user.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
@Transactional
public class AuthServiceImpl extends ServiceImpl<AuthMapper, ApUserRealname> implements AuthService {
    /**
     * 查询用户审核列表
     *
     * @param dto
     */
    @Override
    public ResponseResult lists(AuthDto dto) {
        //1.参数校验
        if(dto == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.分页构造器
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lqw = new LambdaQueryWrapper<>();
        if(dto.getStatus() != null)
            lqw.eq(ApUserRealname::getStatus,dto.getStatus());

        //3.查询结果
        page(page,lqw);

        //4.封装结果返回
        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        result.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        result.setErrorMessage(AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 管理员审核用户
     */
    @Override
    public ResponseResult passOrfail(AuthDto dto, Short type) {
        //1.参数校验
        if(dto == null || dto.getId() == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        //2.更新状态
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());//通过id来更新表，此id与user_id相同
        apUserRealname.setStatus(type);
        if(dto.getMsg() != null)
            apUserRealname.setReason(dto.getMsg());

        updateById(apUserRealname);

        //3.如果通过审核，则需要开通自媒体账号（该账号的用户名和密码与app一致）
        if(type == UserConstants.AUTH_PASS)
            createWmUser(dto);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private ApUserService apUserService;
    @Autowired
    private IWemediaClient iWemediaClient;

    /**
     * 开通自媒体账号
     * */
    private void createWmUser(AuthDto dto) {
        //获取用户信息 ap_user,最后修改状态为 自媒体人：1
        ApUser user = apUserService.getById(dto.getId());

        //新建自媒体用户 wm_user，该账号的用户名和密码与app一致
        WmUser wmUser = new WmUser();
        wmUser.setApUserId(user.getId());
        wmUser.setName(user.getName());
        wmUser.setPassword(user.getPassword());
        wmUser.setSalt(user.getSalt());
        wmUser.setStatus(UserConstants.WM_USER_STATUS_PASS);//状态 正常可用
        wmUser.setType(UserConstants.WM_USER_TYPE_PERSON);//账号类型 个人
        wmUser.setCreatedTime(new Date());

        iWemediaClient.saves(wmUser);

        //修改用户为 自媒体人：1
        user.setFlag(UserConstants.AP_USER_FLAG_WM);
        apUserService.updateById(user);

    }


}
